package com.qf.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsAdmin;
import com.qf.service.DtsAdminService;
import com.qf.util.AdminResponseCode;
import com.qf.util.AdminResponseUtil;
import com.qf.util.AuthSupport;
import com.qf.util.ResponseUtil;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统管理
 *   -- 管理员管理
 * @author lixu
 */
@RestController
@RequestMapping("/admin/admin")
@CrossOrigin
public class AdminController {


    @Autowired
    private DtsAdminService dtsAdminService;
    /**
     * 管理员列表   要求分页
     * - 接口地址 : http://localhost:8083/admin/admin/list?page=1&limit=20&sort=add_time&order=desc
     * - GET请求
     */
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "查询")
    @RequiresPermissions(value = "admin:admin:list")
    @GetMapping("/list")
    public Object adminList(Integer page,Integer limit,String sort,String order,String username){
        //MybatisPlus 分页
        IPage<DtsAdmin> ipage = dtsAdminService.findPageByQuery(page,limit,sort,order,username);
        Map<String,Object> data  = new HashMap<>();
        data.put("total",ipage.getTotal());
        data.put("items",ipage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * /admin/update
     * 编辑管理员
     */
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "编辑")
    @RequiresPermissions(value = "admin:admin:update")
    @PostMapping("/update")
    public Object adminUpdate(@RequestBody DtsAdmin dtsAdmin){

        //修改
        dtsAdminService.update(dtsAdmin);


        return ResponseUtil.ok();
    }
    /**
     * 添加管理员账户
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员" }, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsAdmin admin) {

        String username = admin.getUsername();
        List<DtsAdmin> adminList = dtsAdminService.findAdmin(username);
        if (adminList.size() > 0) {
            return AdminResponseUtil.fail(AdminResponseCode.ADMIN_NAME_EXIST);
        }

        String rawPassword = admin.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(rawPassword);
        admin.setPassword(encodedPassword);
        dtsAdminService.add(admin);

        return ResponseUtil.ok(admin);
    }

    /**
     * 删除管理员账户
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = { "系统管理", "管理员" }, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsAdmin admin) {
        Integer anotherAdminId = admin.getId();
        if (anotherAdminId == null) {
            return ResponseUtil.badArgument();
        }
        dtsAdminService.deleteById(anotherAdminId);
        return ResponseUtil.ok();
    }

}
